home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / libg++ / proto-kit / prototype < prev    next >
Text File  |  1991-06-28  |  4KB  |  177 lines

  1. #!/usr/local/bin/gawk -f
  2. #
  3. # types: types[x] = 1 for each type 'x' we have completed.  'x' has separators
  4. #
  5. # prototypes: 
  6. #    prototypes[x] = the number of dependent types 
  7. #            (x is base type string -- without variables. eg. AVLMap)
  8. #    prototypes[x,"variables"] = the type string with period separated variables
  9. #    prototypes[x,y] = a dependent type (y integer)
  10. #
  11. # lib_prototype[x] = 1
  12. # user_prototype[x] = 1
  13. #    "x" is base type string (eg. AVLMap)
  14. #
  15. # queue: queue of types to be done (with period separators)
  16. #        head: head of queue (where types added)
  17. #        tail: tail of queue (where types are processed)
  18. #
  19.  
  20.  
  21. #
  22. # get_template
  23. #
  24. function get_template (template_string, instance,   res, t_count, t_elements, i_count, i_elements, i) {
  25.     t_count = split (template_string, t_elements, ".");
  26.     i_count = split (instance, i_elements, ".");
  27.  
  28.     for (i = 1 ; i < i_count ; i ++)
  29.       {
  30.         if (i > 1) res = (res ",");
  31.         res = (res t_elements[i] "." i_elements[i]);
  32.       }
  33.     return res;
  34.     }
  35.  
  36. #
  37. # transform: takes an array of the form vars[variable] = value, and returns
  38. #    the instantiation of those variables according to template.
  39. #    "variable" is of the form <xxx>
  40. #
  41. function transform (vars, template,   res, pairs, map, count, i, var, preamble, postamble) {
  42.     res = template;
  43.     count = split(vars, pairs, ",");
  44.  
  45.     for (i = 1 ; i <= count ; i++)
  46.       {
  47.         split(pairs[i],map,".");
  48.         if (match(map[1],"<.+>") > 0)
  49.           {
  50.         var = substr(map[1],RSTART,RLENGTH);
  51.         if (RSTART > 1)
  52.           preamble = substr(map[1],1,RSTART);
  53.         if (RSTART + RLENGTH < length(map[1]))
  54.           postamble = substr(map[1],RSTART+RLENGTH);
  55.  
  56.         if (match(map[2],("^" preamble ".*" postamble "$")) > 0)
  57.           {
  58.             match(map[2],("^" preamble));
  59.             if (RLENGTH > 0)
  60.               map[2] = substr(map[2],RSTART+RLENGTH);
  61.             match(map[2],(postamble "$"));
  62.             if (RLENGTH > 0)
  63.               map[2] = substr(map[2],1,RSTART-1);
  64.  
  65.             gsub(var, map[2], res);
  66.           }
  67.           }
  68.       }
  69.     return res;
  70.     }
  71.  
  72. function is_basic (t) {
  73.     if (basic_types[t] == 1)
  74.       return 1;
  75.     return 0;
  76.     }
  77.  
  78. function add_type (t,   c, e) {
  79.     c = split(t,e,".");
  80.  
  81.     if ((is_basic(t) != 0) || (t ~ /_p$/) || (c <= 1))
  82.       return 0;
  83.     if (match(t,"<.*>") > 0)
  84.       return 0;
  85.  
  86.     types[t] = 1;
  87.     return 1;
  88.     }
  89.  
  90. function get_prototype (type,  count, names) {
  91.     count = split(type, names, ".");
  92.     return names[count];
  93.     }
  94.  
  95. BEGIN {
  96.     head = 1;
  97.     tail = 1;
  98.     }
  99.  
  100. ($1 == "basic-type") || ($1 == "libg++-type") || ($1 == "user-type") { 
  101.     if ($1 == "basic-type")
  102.       basic_types[$2] = 1 ;
  103.     if ($1 == "libg++-type")
  104.       lib_types[$2] = 1;
  105.     if ($1 == "user-type") 
  106.       {
  107.         add_type($2);
  108.         user_types[$2] = 1 ;
  109.         for (i = 3 ; i <= NF ; i++)
  110.           if (add_type($i) > 0)
  111.             {
  112.           queue[head] = $i;
  113.           head ++;
  114.             }
  115.       }
  116.         
  117.     }
  118.  
  119. ($1 == "libg++-prototype") || ($1 == "user-prototype") {
  120.     proto = get_prototype($2);
  121.     prototypes[proto] = NF - 2;
  122.     prototypes[proto,"variables"] = $2;
  123.  
  124.     if ($1 == "libg++-prototype")
  125.       lib_prototypes[proto] = 1;
  126.     if ($1 == "user-prototype")
  127.       user_prototypes[proto] = 1;
  128.  
  129.     for (i = 3 ; i <= NF ; i ++)
  130.         prototypes[proto,i - 2] = $i;
  131.     }
  132.  
  133. $1 == "instantiate" {
  134.     if (add_type($2) > 0)
  135.       {
  136.         queue[head] = $2;
  137.         head ++;
  138.       }
  139.     }
  140.  
  141. END {
  142.     while (head > tail)
  143.       {
  144.         count = split(queue[tail], elements, ".");
  145.         template = get_template(prototypes[elements[count],"variables"], queue[tail]);
  146.  
  147.         for (i = 1 ; i <= prototypes[elements[count]] ; i ++)
  148.           {
  149.             t = transform(template, prototypes[elements[count],i]);
  150.          if (add_type(t))
  151.           {
  152.                 queue[head] = t;
  153.                 head++;
  154.           }
  155.           }
  156.         tail ++;
  157.       }
  158.  
  159.     for (t in types)
  160.       if (types[t] == 1)
  161.         {
  162.           proto = get_prototype(t);
  163.           if ((output_type == "libg++-prototypes") && (lib_prototypes[proto] == 1))
  164.         print t;
  165.           if ((output_type == "user-prototypes") && (user_prototypes[proto] == 1))
  166.         print t;
  167.         }
  168.  
  169.     if (output_type == "libg++-types")
  170.       for (t in lib_types)
  171.         print t;
  172.  
  173.     if (output_type == "user-types")
  174.       for (t in user_types)
  175.         print t;
  176.     }
  177.